package com.yt.order.api.client;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import com.yt.order.models.bo.WorkOrderEvaluationSaveBO;
import com.yt.order.models.dto.WorkOrderInfoDTO;
import com.yt.order.models.service.IWorkOrderInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.yt.order.models.service.IWorkOrderEvaluationService;
import com.yt.order.models.bo.WorkOrderEvaluationBO;
import com.yt.order.models.dto.WorkOrderEvaluationDTO;

import java.util.Date;

/**
 * 订单评价控制器
 *
 * @author yzz
 * @Date 2025-09-15 14:06:29
 */

@RestController
@RequestMapping("/api/workOrderEvaluation")
@Slf4j
@Api(value = "WorkOrderEvaluationApiController",tags = "订单评价Api接口")
public class WorkOrderEvaluationApiController {

    @DubboReference
    private IWorkOrderEvaluationService workOrderEvaluationService;

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<WorkOrderEvaluationDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        WorkOrderEvaluationDTO dto = workOrderEvaluationService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "订单评价列表",notes = "订单评价列表")
    @PostMapping("/queryPageList")
    public Result<Page<WorkOrderEvaluationDTO>> queryPageList(WorkOrderEvaluationBO bo){
        Wrapper<WorkOrderEvaluationDTO> wrapper = new EntityWrapper<>();
        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<WorkOrderEvaluationDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = workOrderEvaluationService.selectPage(page, wrapper);

        return new Result<>(page);
    }

    @PostMapping("/getByWorkOrderId")
    @ApiOperation(value = "根据工单ID查找",notes = "根据工单ID查找")
    @ApiImplicitParam(name = "workOrderId",value = "工单ID",required = true,paramType = "query", dataType = "Long")
    public Result<WorkOrderEvaluationDTO> getByWorkOrderId(@RequestParam("workOrderId") Long workOrderId){
        if(workOrderId == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        Wrapper<WorkOrderEvaluationDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("work_order_id", workOrderId);
        WorkOrderEvaluationDTO dto = workOrderEvaluationService.selectOne(wrapper);

        return new Result<>(dto);
    }

    @Autowired
    private IWorkOrderInfoService workOrderInfoService;

    @ApiOperation(value = "添加订单评价",notes = "添加订单评价")
    @PostMapping(value = "/add")
    public Result add(@RequestBody WorkOrderEvaluationSaveBO saveBO){
        try {
            boolean result = workOrderEvaluationService.add(saveBO);
            if(result){
                // 评价添加成功后，将对应工单状态改为4
                if (saveBO.getWorkOrderId() != null) {
                    WorkOrderInfoDTO workOrderInfoDTO = new WorkOrderInfoDTO();
                    workOrderInfoDTO.setId(saveBO.getWorkOrderId());
                    workOrderInfoDTO.setStatus(4);
                    workOrderInfoDTO.setUpdateTime(new Date()); // 更新时间
                    workOrderInfoService.updateById(workOrderInfoDTO);
                }
                return new Result();
            }
            return new Result<>(CodeEnum.FAIL_100003);
        } catch (RuntimeException e) {
            return new Result<>(CodeEnum.FAIL_100003.getCode(), e.getMessage());
        }
    }

    @ApiOperation(value = "修改订单评价",notes = "修改订单评价")
    @PostMapping("/update")
    public Result update(WorkOrderEvaluationSaveBO saveBO){
        WorkOrderEvaluationDTO dto = new WorkOrderEvaluationDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setId(saveBO.getId());
        boolean flag = workOrderEvaluationService.updateById(dto);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    @ApiOperation(value = "删除订单评价",notes = "删除订单评价")
    @ApiImplicitParam(name = "id",value = "订单评价id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id){
        boolean flag = workOrderEvaluationService.deleteById(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @ApiOperation(value = "修改评价状态",notes = "修改评价状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "订单评价id",required = true,paramType = "query",dataType = "Long"),
            @ApiImplicitParam(name = "isValid",value = "评价状态（0无效，1有效）",required = true,paramType = "query",dataType = "Integer")
    })
    @PostMapping("/updateIsValid")
    public Result updateIsValid(@RequestParam("id") Long id, @RequestParam("isValid") Integer isValid){
        // 参数校验
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002.getCode(), "订单评价ID不能为空");
        }
        
        if(isValid == null){
            return new Result<>(CodeEnum.FAIL_100002.getCode(), "评价状态不能为空");
        }
        
        if(isValid != 0 && isValid != 1){
            return new Result<>(CodeEnum.FAIL_100002.getCode(), "评价状态只能为0(无效)或1(有效)");
        }

        WorkOrderEvaluationDTO dto = new WorkOrderEvaluationDTO();
        dto.setId(id);
        dto.setIsValid(isValid);
        boolean flag = workOrderEvaluationService.updateById(dto);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

}
